<?php

class Application_Form_New extends Application_Form_Basic
{

    public function init()
    {
        $this ->setMethod ( 'post' );
        
        $this->addElement(
            'hidden', 
            'id', 
            array(
                'value' => null
            )
        );
				
		$this->addElement ( 
            'text',
            'title',
            array ( 
                'label' => 'Заголовок:',
                'required' => true,
                'validators' => array(
                    array ('NotEmpty', true, 
                        array (
                            'messages' => array (
                                'isEmpty' => 'Поле не може бути пустим!' 
                            )
                        ) 
                    ),
                    array('stringLength', false, array(10, 180))
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'title' ),
			'title_group'
		);
        
        $group = $this->getDisplayGroup ('title_group');
        $group->removeDecorator ('DtDdWrapper');
		
		$this->addElement ( 
            'textarea', 
            'description', 
            array ( 
                'label' => 'Опис:',
                'required' => true,
                'validators' => array(
                    array ('NotEmpty', true, 
                        array (
                            'messages' => array (
                                'isEmpty' => 'Поле не може бути пустим!' 
                            )
                        ) 
                    ),
                    array('stringLength', false, array('min' => 10))
                )
            )
        );
        
        $this->addDisplayGroup(
			array( 'description' ),
			'description_group'
		);
        
        $group = $this->getDisplayGroup ('description_group');
        $group->removeDecorator ('DtDdWrapper');
		
        $this->addElement('file', 'photos', array(
			'label' => 'Фото',
			'destination' => Application_Model_PathsService::getInstance()->get("temp"),
			'valueDisabled' => true,
			'Count' => array(1),
            /*'validators' => array(
                array('ExcludeMimeType', false, array('image/gif',
                                                      'image/jpeg',
                                                      'image/png'))
            )*/
		));
        
        $this->addDisplayGroup(
			array( 'photos' ),
			'photos_group'
		);
        
        $group = $this->getDisplayGroup ('photos_group');
        $group->removeDecorator ('DtDdWrapper');
        
		$this->addElement ( 
            'submit', 
            'submit', 
            array (
                'ignore' => true, 
                'label' => 'Добавити' 
            ) 
        );
        
        $this->addDisplayGroup(
			array( 'submit' ),
			'buttos_group',
            array(
                'class' => 'group_buttons'
            )
		);
        
        $group = $this->getDisplayGroup ('buttos_group');
        $group->removeDecorator ('DtDdWrapper');
    }
    
    public function processUploadedFiles() {
		/*@var $adapter Zend_File_Transfer_Adapter_Http */
		$adapter =  $this->photos->getTransferAdapter();
		$receivingOK = true;
		foreach ($adapter->getFileInfo() as $file => $info) {
		    if($info['name']){
		        $originalFilename = pathinfo($info['name']);
			    $adapter->addFilter('Rename', md5(uniqid(rand(),1)) . '-' . substr($originalFilename['filename'], 0, 10) . '.' . $originalFilename['extension'], $file);
			    if (!$adapter->receive($file)) {
			         $receivingOK = false;
			    }
		    }
		}

		if(!$receivingOK){
			throw new Exception('Error uploading files!');
		}

		$this->photos->receive();
	}
}

